﻿///<reference path="../jquery/jquery.js"/>
var Topic = function() {
	//private area
	var _ajaxDefaultOpts = {
		type: 'post',
		dataType: 'json',
		contentType: 'application/json;charset=utf-8'
	};
	var _doSave = function() {
		$("#btnSave").html("保存中...").attr("disabled", true);
		//get all inputs for category edit
		var data = $(":input").filter("[name^='item']").serializeArray();
		//Topic body
		var topicBody = {};
		topicBody.name = "itemContent";
		topicBody.value = tinyMCE.activeEditor.getContent();
		data.push(topicBody);
		data.push({ name: "itemTopicID", value: Topic.TopicID });
		//requset to server
		var ajaxopts = _ajaxDefaultOpts;
		ajaxopts.url = Topic.Asmx + "AddComment";
		ajaxopts.data = $.toJSON({ Params: data });
		ajaxopts.success = function(msg) {
			$("#btnSave").html("保 存").attr("disabled", false);
			msg = msg.d;
			if (msg.IsOk) {
				Topic.SelectComments($.evalJSON(Topic.InitPager));
			} else {
				alert(msg.Body);
			}
		};
		$.ajax(ajaxopts);
		ajaxopts = null;
	};
	var _captchaCallback = function(arg, context) {
		if (arg == "1") {
			return;
		} else {
			_doSave();
		};
	};
	var _updateCommentsNum = function(num) {
		$("div.replaynum", "topicview").eq(0).children().text("评论数:[" + num + "]");
	};
	//public areas
	return {
		InitPager: "",
		Asmx: "",
		TopicID: "",
		ViewPath: "~/Views/TopicCommentsView.ascx",
		Init: function(opts) {
			Topic.Asmx = opts.Asmx || "";
			Topic.TopicID = opts.TopicID || "";
			Topic.InitPager = opts.InitPagerStr || "";
			Topic.ViewPath = opts.ViewPath || Topic.ViewPath;
			$("#btnSave").click(function() {
				Topic.NewComment({});
			});
			//Do first load.
			Topic.SelectComments($.evalJSON(Topic.InitPager));
		}, //endof Init
		DefaultDialogOpts: {
			modal: true,
			width: 600,
			height: 400,
			resizable: false,
			overlay: { opacity: 0.5, background: 'black' }
		},
		PreEdit: function(id, opts) {
			if (id != -1) {
				//prepare ajax opts
				var ajaxOpts = _ajaxDefaultOpts;
				ajaxOpts.url = Topic.Asmx + "PreEditComment";
				ajaxOpts.data = $.toJSON({ Params: { id: id} });
				ajaxOpts.success = function(data) {
					data = data.d;
					$("#txtTitle").val(data.Title).select();
					$("#txtEMail").val(data.EMail);
					$("#txtName").val(data.Name);
					$("#txtDataKey").val(id);
					//Topic body
					tinyMCE.setContent(data.Content);
					if (opts.popup) {
						Topic.DefaultDialogOpts.close = function() {
							$("#quickreplyboxWrapper").append($("#quickreplybox").dialog("destroy").remove());
						};
						$("#quickreplybox").dialog(Topic.DefaultDialogOpts);
					};
				}
				//Request to server
				$.ajax(ajaxOpts);
				ajaxOpts = null;
			}
		}, //endof preEdit
		DelComment: function(opts) {
			var key = opts.id;
			if (!window.confirm("确认删除选定记录么?")) {
				return;
			};
			//Request to server
			var ajaxOpts = _ajaxDefaultOpts;
			ajaxOpts.url = Topic.Asmx + "RemoveComment";
			ajaxOpts.data = $.toJSON({ Params: { id: key, TopicID: Topic.TopicID} });
			ajaxOpts.success = function(data) {
				//ToDo:Remove A comment
				alert("删除功能正在开发中");
			};
			$.ajax(ajaxOpts);
			ajaxOpts = null;
		}, //endof delete
		NewComment: function(opts) {
			//anti-messy reply
			WebForm_DoCallback('ctl00$cphBody$quickReply1', $("#hfCaptcha1").val(), _captchaCallback, 'topiccomment', null, false);
		}, //Endof NewComment
		SelectComments: function(opts) {
			var data = {};
			data.CPage = opts.CurrentPage;
			data.RPPage = opts.RowPerPage;
			data.TopicID = Topic.TopicID;
			data.ViewPath = Topic.ViewPath;
			var ajaxOpts = _ajaxDefaultOpts;
			ajaxOpts.url = Topic.Asmx + "SelectComments";
			ajaxOpts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					//Replace Topic
					$("#commentsWrapper").html(msg.Context.CommentListHtml);
					//Replace pager
					$("#pagerWrapper").html(msg.Context.PagerHtml);
					//Init pager again
					SimplePager.Init(msg.Context.Pager, Topic.SelectComments);
					//scroll to the top record
					$.scrollTo("#id_"+Topic.TopicID);
					//if (msg.Context.TopItemID) { //see to "TopicCommentView.ascx"
					//	location.hash = "id_topiccomment" + msg.Context.TopItemID;
					//};
					
					//update comments num
					if (msg.Context.CommentsNum) {
						_updateCommentsNum(msg.Context.CommentsNum);
					}
				} else {
					alert("Something goes wrong!");
				}
			};
			ajaxOpts.data = $.toJSON({ Params: data });
			$.ajax(ajaxOpts);
		}, //endof Select
		SetTags: function(opts) {
			var data = {};
			data.id = opts.id;
			data.tags = opts.tags;
			data.TopicViewPath = opts.topicViewPath || "~/Views/TopicView.ascx";
			var ajaxopts = _ajaxDefaultOpts;
			ajaxopts.url = Topic.Asmx + "UpdateTags";
			ajaxopts.data = $.toJSON({ Params: data });
			ajaxopts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					$("#topicviewWrapper").html(msg.Context.TopicViewHtml).hide().fadeIn();
				} else {
					alert("请稍候重试！");
				}
			};
			$.ajax(ajaxopts);
		} //endof SetTags
	};
} ();